package com.game.jumper.activity;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import net.youmi.android.banner.AdSize;
import net.youmi.android.banner.AdView;
import net.youmi.android.diy.banner.DiyAdSize;
import net.youmi.android.diy.banner.DiyBanner;
import net.youmi.android.smart.SmartBannerManager;
import net.youmi.android.spot.SpotManager;

import org.apache.http.util.EncodingUtils;
import org.loon.framework.android.game.LGameAndroid2DActivity;

import android.R.bool;
import android.app.AlertDialog;
import android.app.Service;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.Toast;

import com.example.jumperball.R;
import com.game.jumper.game.Game;

public class PlayLG extends LGameAndroid2DActivity {

	@Override
	public void onGamePaused() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		for (Bitmap element : PlayLG.backimage) {
			if (element == null)
				continue;
			if (element.isRecycled() == false) // 如果没有回收
				element.recycle();
		}
	}

	long mExitTime;

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		AudioManager audioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			if ((System.currentTimeMillis() - mExitTime) > 2000) {
				// Object mHelperUtils;
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();

			} else {
				System.exit(0);

			}
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
			audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_RAISE,
					AudioManager.FX_FOCUS_NAVIGATION_UP);
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
			audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_LOWER,
					AudioManager.FX_FOCUS_NAVIGATION_UP);
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onGameResumed() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMain() {
		// TODO Auto-generated method stub
		/* 设置游戏是否横屏 */

		this.initialization(false);

		/* 给Activity赋予一个画面，有点类似于View */

		this.setScreen(new PlayScreen());

		/* 这个厉害了，指定游戏的帧速率，每秒30帧 */

		this.setFPS(30);

		/* 是否显示游戏帧数（当然显示喇） */

		this.setShowFPS(false);

		/* 不显示Logo（这个没试过显示，先不管，我们要的是HelloWorld） */

		// this.setShowLogo(false);

		/* OK，一切准备就绪，显示游戏画面 */
		// SpotManager.getInstance(this).loadSpotAds();
		// //SpotManager.getInstance(PlayLG.thLg).showSpotAds(PlayLG.thLg);
		// SpotManager.getInstance(this).showSpotAds(this);
		thLg = this;
		this.showScreen();

		init();
		// 设定调整音量为媒体音量,当暂停播放的时候调整音量就不会再默认调整铃声音量了
		this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.main, menu);
		menu.add(0, 1, 1, "关于").setIcon(R.drawable.jumperballlogo);
		return true;
	}
	  public boolean onOptionsItemSelected(MenuItem item) {
		  if(item.getGroupId()==0){
		  new AlertDialog.Builder(this)  
		                  .setTitle("6425工作室")
		                  .setMessage("欢迎您提出宝贵的意见和建议，发送邮件至weilaizhimen@126.com")
		                  .setPositiveButton("确定", null)
		                  .show();
		  
		  }
	        return true; 
	    }

	public AdView adView;
	boolean adflag;

	public void showad(Boolean show) {
		if (show) {
			if(!adflag){

				 adflag=true;
				 }
			adView.setVisibility(View.VISIBLE);
		} else {
			if(adView!=null)
			adView.setVisibility(View.INVISIBLE);
		}
	}

	// 背景
	public static Bitmap[] backimage = new Bitmap[9];

	public static Bitmap ballBitmap;
	
	// 声明SoundPool
	private static SoundPool sp;

	public static int soundboard;

	public static int sounddown;

	public static int soundfly;

	public static int soundup;

	public static int sounddead;

	public static PlayLG thLg;

	void init() {
		backimage[0] = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.wall);
		backimage[1] = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.f002);
		backimage[2] = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.f003);
		backimage[3] = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.f004);
		backimage[4] = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.f005);
		backimage[5] = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.f006);
		backimage[6] = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.f007);
		backimage[7] = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.f008);
		backimage[8] = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.f009);
		ballBitmap=BitmapFactory.decodeResource(this.getResources(),
				R.drawable.ball);
		// 实例SoundPool播放器
		sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
		// 加载音乐文件获取其数据ID
		soundboard = sp.load(this, R.raw.board, 1);
		// 加载音乐文件获取其数据ID
		sounddown = sp.load(this, R.raw.down, 1);

		soundfly = sp.load(this, R.raw.fly, 1);
		soundup = sp.load(this, R.raw.up, 1);
		sounddead = sp.load(this, R.raw.dead, 1);

		try {
			String sg = PlayLG.thLg.readFileData("goals.txt");
			Game.maxGoals = Integer.parseInt(sg);
		} catch (Exception e) {

		}
	}

	public static void playsound(int soundid) {
		sp.play(soundid, 2, 2, 0, 0, 1);
	}

	// 写文件在./data/data/com.tt/files/下面

	public void writeFileData(String fileName, String message) {

		try {

			FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);

			byte[] bytes = message.getBytes();

			fout.write(bytes);

			fout.close();

		}

		catch (Exception e) {

			e.printStackTrace();

		}

	}

	// -------------------------------------------------------

	// 读文件在./data/data/com.tt/files/下面

	public String readFileData(String fileName) {

		String res = "";

		try {

			FileInputStream fin = openFileInput(fileName);

			int length = fin.available();

			byte[] buffer = new byte[length];

			fin.read(buffer);

			res = EncodingUtils.getString(buffer, "UTF-8");

			fin.close();

		}

		catch (Exception e) {

			e.printStackTrace();

		}

		return res;

	}

}
